from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
import os

driver = webdriver.Chrome()
# 实例链接路径
url = 'file:///' + os.path.abspath('html') + '/注册实例.html'
driver.get(url)
driver.maximize_window()

# 定位注册下拉框
s1 = driver.find_element(By.ID, 'select')
time.sleep(2)
Select(s1).select_by_index(1)
time.sleep(2)
Select(s1).select_by_value('gz')
time.sleep(2)
Select(s1).select_by_visible_text('上海')
# 定位弹框按钮，并点击
driver.find_element(By.ID, 'alert').click()
# 切换到弹框，并点击
alert1 = driver.switch_to.alert
alert1.accept()

# 切换到注册A框架
driver.switch_to.frame('myframe1')
# 滑动鼠标
js = 'window.scrollTo(0, 1500)'
driver.execute_script(js)
# 定位注册下拉框
s2 = driver.find_element(By.ID, 'selectA')
time.sleep(2)
Select(s2).select_by_index(1)
time.sleep(2)
Select(s2).select_by_value('gz')
time.sleep(2)
Select(s2).select_by_visible_text('A重庆')
# 定位弹框按钮，并点击
driver.find_element(By.ID, 'alerta').click()
# 切换到弹框，并点击
alert2 = driver.switch_to.alert
alert2.accept()

# 切换到注册B框架
driver.switch_to.parent_frame()
driver.switch_to.frame('myframe2')
# 滑动鼠标
js = 'window.scrollTo(0, 1500)'
driver.execute_script(js)
# 定位注册下拉框
s3 = driver.find_element(By.ID, 'selectB')
time.sleep(2)
Select(s3).select_by_index(1)
time.sleep(2)
Select(s3).select_by_value('gz')
time.sleep(2)
Select(s3).select_by_visible_text('上海B')
# 定位弹框按钮，并点击
driver.find_element(By.ID, 'alertB').click()
# 切换到弹框，并点击
alert3 = driver.switch_to.alert
alert3.accept()

# 退出浏览器
time.sleep(2)
driver.quit()
